﻿using MvvmCross.Core.ViewModels;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Sidvall.AuthorizationManager.UI.ViewModels
{
	public partial class UserConfigurationViewModel
	{
		#region Public Members

		#region PageTypes

		private Sidvall.Data.KeyValueCollection<string, Sidvall.AuthorizationManager.Data.PageType> _PageTypes;
		public Sidvall.Data.KeyValueCollection<string, Sidvall.AuthorizationManager.Data.PageType> PageTypes
		{
			get
			{
				if (_PageTypes == null)
					this.PageTypes = Sidvall.Data.KeyValueManager.FromEnum<Sidvall.AuthorizationManager.Data.PageType>();
				return _PageTypes;
			}
			set
			{
				_PageTypes = value;
				RaisePropertyChanged("PageTypes");
			}
		}

		#endregion
		#region CanConnectToServer

		public bool CanConnectToServer
		{
			get
			{
				return Sidvall.AuthorizationManager.Security.SecurityManager.CanConnectToServer;
			}
		}

		#endregion

		// Commands
		#region ExportSettingsCommand

		public ICommand ExportSettingsCommand
		{
			get
			{
				return new MvxCommand(async () => await ExportSettingsAsync());
			}
		}

		#endregion
		#region ExportSettingsAsync

		public async Task ExportSettingsAsync()
		{
			if (this.ActiveEntity == null)
				return;
			var item = new Net.Services.V1.UserConfiguration();
			item.Import(this.ActiveEntity, Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.None, null));
			await ViewModelManager.ExportSettingsAsync("UserConfiguration.txt", item);
		}

		#endregion
		#region ImportSettingsCommand

		public ICommand ImportSettingsCommand
		{
			get
			{
				return new MvxCommand(async () => await ImportSettingsAsync());
			}
		}

		#endregion
		#region ImportSettingsAsync

		public async Task ImportSettingsAsync()
		{
			Task task = null;
			if (this.ActiveEntity == null)
				return;
			try
			{
				var content = await ViewModelManager.ImportSettingsAsync();
				if (string.IsNullOrWhiteSpace(content))
					return;
				var item = Sidvall.AuthorizationManager.UI.SystemContext.Current.LoadUserConfiguration(content);
				this.ActiveEntity = item.Copy(Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.StateAndKey, null));
				this.ActiveEntity = await Sidvall.AuthorizationManager.UI.SystemContext.Current.SaveUserConfigurationAsync(this.ActiveEntity, true);
			}
			catch (System.Exception ex)
			{
				task = Sidvall.UI.SystemContext.Current.DialogManager.ShowErrorMessageAsync("Error", ex);
			}
			if (task != null)
				await task;
		}

		#endregion
		#region RefreshItemsCommand

		public ICommand RefreshItemsCommand
		{
			get
			{
				return new MvxCommand(async () => await RefreshItemsAsync());
			}
		}

		#endregion
		#region RefreshItemsAsync

		public async Task RefreshItemsAsync()
		{
			Task task = null;
			using (var busyManager = new Sidvall.Data.BusyManager(this))
			{
				try
				{
					this.ActiveEntity = await Sidvall.AuthorizationManager.UI.SystemContext.Current.LoadUserConfigurationAsync();
				}
				catch (System.Exception ex)
				{
					task = Sidvall.UI.SystemContext.Current.DialogManager.ShowErrorMessageAsync("Error", ex);
				}
				if (task != null)
					await task;
			}
		}

		#endregion
		#region ConnectToServerCommand

		public ICommand ConnectToServerCommand
		{
			get
			{
				return new MvxCommand(async () => await ConnectToServerAsync());
			}
		}

		#endregion
		#region ConnectToServerAsync

		public async Task ConnectToServerAsync()
		{
			Task task = null;
			if (!this.CanConnectToServer)
				return;
			using (var busyManager = new Sidvall.Data.BusyManager(this))
			{
				try
				{
					await Sidvall.AuthorizationManager.UI.SystemContext.Current.LoadServerInfoAsync(true, true);
					await DoLoadAsync();
				}
				catch (System.Exception ex)
				{
					task = Sidvall.UI.SystemContext.Current.DialogManager.ShowErrorMessageAsync("Error", ex);
				}
				if (task != null)
					await task;
			}
		}

		#endregion

		#region LoadItemAsync

		protected override Task<Entities.UserConfiguration> LoadItemAsync()
		{
			var item = Sidvall.AuthorizationManager.UI.SystemContext.Current.UserConfiguration.Copy(Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.StateAndKey, null));
			return Task.FromResult(item);
		}

		#endregion
		#region SaveItemAsync

		protected override async Task<Entities.UserConfiguration> SaveItemAsync(Entities.UserConfiguration item)
		{
			item = await Sidvall.AuthorizationManager.UI.SystemContext.Current.SaveUserConfigurationAsync(item, false);
			return item.Copy(Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.StateAndKey, null));
		}

		#endregion
		#region StartAsync

		protected override async Task StartAsync()
		{
			await DoLoadAsync();
		}

		#endregion

		#endregion
	}
}
